interface CookieOptions {
  expires?: number
  path?: string
  domain?: string
  secure?: boolean
  httpOnly?: boolean
  sameSite?: 'lax' | 'strict' | 'none'
}
/**
 * @Author: Hisun 958306134@qq.com
 * @LastEditors: Hisun 958306134@qq.com
 * @Date: 2024-07-10 23:40:05
 * @Description: js封装cookie类, 默认有效期{days = 1}天
 */

class CookieTool {
  public static setCookie(
    key: string,
    value: string,
    options: CookieOptions = {},
  ) {
    if (typeof document === 'undefined') throw new Error('暂不支持cookie')
    let cookieStr = `${key}=${encodeURIComponent(value)}`
    if (options.expires) {
      const nowDate = new Date()
      const days = options.expires * 24 * 60 * 60 * 1000
      nowDate.setTime(nowDate.getTime() + days)
      cookieStr += '; expires=' + nowDate.toUTCString()
    }
    if (options.path) {
      cookieStr += '; path=' + options.path
    }
    if (options.domain) {
      cookieStr += '; domain=' + options.domain
    }

    if (options.secure) {
      cookieStr += '; secure'
    }

    if (options.httpOnly) {
      cookieStr += '; httpOnly'
    }

    if (options.sameSite) {
      cookieStr += '; sameSite=' + options.sameSite
    }
    document.cookie = cookieStr
  }
  private static getCookie(name: string): string | null {
    if (typeof document === 'undefined') throw new Error('暂不支持cookie')
    const nameEQ = name + '='
    const cookies = document.cookie.split(';')
    for (let i = 0, len = cookies.length; i < len; i++) {
      let c = cookies[i]
      while (c.charAt(0) === ' ') c = c.substring(1, c.length)
      if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length)
    }
    return null
  }
  set(name: string, value: any, options: CookieOptions = {}) {
    const cookieValue =
      typeof value === 'object' ? JSON.stringify(value) : value
    CookieTool.setCookie(name, cookieValue, options)
  }
  get(name: string): string | null {
    const value = CookieTool.getCookie(name)
    if (!value) return null
    try {
      return JSON.parse(decodeURIComponent(value))
    } catch (e) {
      return value
    }
  }
  remove(name: string): void {
    CookieTool.setCookie(name, '', { expires: 0 })
    // document.cookie = name + '=; Max-Age=-99999999;'
  }
  getAll(): { [key: string]: string } {
    const cookies: { [key: string]: string } = {}
    const ca = document.cookie.split(';')
    for (let i = 0, len = ca.length; i < len; i++) {
      let c = ca[i]
      while (c.charAt(0) === ' ') c = c.substring(1, c.length)
      const [key, value] = c.split('=')
      if (key && value) {
        cookies[key] = value
      }
    }
    return cookies
  }
}
export const cookie = new CookieTool()

export const _cookieWebsite = {
  token: 'authorization',
  refreshToken: 'refreshToken',
  language: 'language',
}
export const languageKey = 'lang'
export function getLanguage() {
  return cookie.get(_cookieWebsite.language)
}
export function getToken() {
  return cookie.get(_cookieWebsite.token)
}
export function removeToken() {
  return cookie.remove(_cookieWebsite.token)
}
export function removeLanguage() {
  return cookie.remove(_cookieWebsite.language)
}
export function isLogin() {
  const token = getToken
  return token !== null && token !== undefined
}

export default cookie
